#!BPY

# Name: 'Export to GAT'
# Blender: '243'
# Group: 'Export'

# Simple script exporting an active object to the GAT (Geometry and Topology) format.
import Blender
import sys


def exportActiveObject(outFilename):

	objs = Blender.Object.GetSelected()
	
	#print "Export begins..."
	#print type(objs)
	#print objs
	#print type(objs[0])
	#print objs[0]
	
	if len(objs) > 0:
		
		fout = open(outFilename, 'w')
		
		vert2index = {}
		trcount = 0
	
		for ob in objs:
			#print type(ob)
			mesh = ob.getData(False,True)
			print type(mesh)
			
			# Check whether mesh is a Mesh
			end = len(mesh.verts)
			
			# Number of vertices
			fout.write("vertices\n")
			fout.write('%i\n' % (end))
			
			
			i = 0
			while i < end:
				#print mesh.verts[i].co[0]
				#print mesh.verts[i].co[1]
				#print mesh.verts[i].co[2]
				#print mesh.verts[i].no.x
				#print mesh.verts[i].no.y
				#print mesh.verts[i].no.z
				fout.write('%f %f %f\n' % (mesh.verts[i].co[0], mesh.verts[i].co[1], mesh.verts[i].co[2]))
				vert2index[mesh.verts[i]] = i
				i = i+1	
			fout.write("\n")
			
			fout.write("normal vectors\n")
			fout.write('%i\n' % (end))
			i = 0
			while i < end:
				fout.write('%f %f %f\n' % (mesh.verts[i].no.x, mesh.verts[i].no.y, mesh.verts[i].no.z))
				i = i+1
			fout.write("\n")
			
			end = len(mesh.materials)
			fout.write("materials\n")
			fout.write("%i\n" % end)
			
			if end == 0:
				fout.write("\n")
			
			i = 0
			while i < end:
				mat = mesh.materials[i]
				#print mat.rgbCol[0]
				#print mat.rgbCol[1]
				#print mat.rgbCol[2]
				#print mat.specCol[0]
				#print mat.specCol[1]
				#print mat.specCol[2]
				fout.write("diffuse %f %f %f\n" % (mat.R, mat.G, mat.B))
				fout.write("specular %f %f %f\n" % (mat.specR, mat.specG, mat.specB))
				fout.write("\n")
				i = i+1
			
			
			end = len(mesh.faces)
			tris = list()
			
			i = 0
			while i < end:
				vnum = len(mesh.faces[i])
				if vnum == 3: # triangle
					tris.append((vert2index[mesh.faces[i].verts[0]], vert2index[mesh.faces[i].verts[1]], vert2index[mesh.faces[i].verts[2]]))
					trcount = trcount + 1
				elif vnum == 4: # square
					# Split the square into two triangles
					tris.append((vert2index[mesh.faces[i].verts[0]], vert2index[mesh.faces[i].verts[1]], vert2index[mesh.faces[i].verts[2]]))	
					tris.append((vert2index[mesh.faces[i].verts[2]], vert2index[mesh.faces[i].verts[3]], vert2index[mesh.faces[i].verts[0]]))
					trcount = trcount + 2
				else: # it should never happen
					fout.write("This file is corrupted")
					fout.close()
					print "ERROR, expected a triangle or a quad but a polygon with %i faces seen instead." % vnum
					return 1
				i = i+1
			
		
			fout.write("triangles\n")
			fout.write("%i\n" % (trcount))	
			i = 0
			while i < trcount:
				fout.write("%i %i %i\n" % (tris[i][0], tris[i][1], tris[i][2]))
				i = i+1
			
			#i = 0
			#while i < end:
				#vnum = len(mesh.faces[i])
				#if vnum == 3: # triangle
					#fout.write('%i %i %i\n' % (vert2index[mesh.faces[i].verts[0]], vert2index[mesh.faces[i].verts[1]], vert2index[mesh.faces[i].verts[2]]))	
					#trcount = trcount + 1
					
				#elif vnum == 4: # square
					##print "hello square"
					## Split the square into two triangles
					#fout.write('%i %i %i\n' % (vert2index[mesh.faces[i].verts[0]], vert2index[mesh.faces[i].verts[1]], vert2index[mesh.faces[i].verts[2]]))
						
					#fout.write('%i %i %i\n' % (vert2index[mesh.faces[i].verts[2]], vert2index[mesh.faces[i].verts[3]], vert2index[mesh.faces[i].verts[0]]))
					#trcount = trcount + 2
					
				#else: # it should never happen
					#fout.write("This file is corrupted")
					#fout.close()
					#print "ERROR, expected a triangle or a quad but a polygon with %i faces seen instead." % vnum
					#return 1
				##print "mat index = %i" % mesh.faces[i].mat
				#i = i+1
				
			# Export only the active object, even if multiple objects are selected
			# (active object is the first one on the list of objects).
			break
		
		#fout.write('%i\n' % (trcount))
		
		fout.close()
		
		print "Export ends"
	

	
		
Blender.Window.FileSelector(exportActiveObject, "Export file")
